/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/PGLStackedLine2D.h>
#include <pgl/core/PGLContainer.h>
#include <pgl/core/PGLPie.h>
#include <pgl/core/PGLBar.h>
#include <pgl/core/PGLRegion.h>
#include <pgl/core/PGLLegend.h>
#include <pgl/core/PGLObjectManager.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

void CPGLObjectManager::SerializeXML(CPGLArchive& ar)
{
	CString strPos;	
	strPos.Format(_T("objectPos%u"),(DWORD)this);

	Container::iterator it;

	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if (ar.IsStoring())
	{
		// copying
		for (it = m_objects.begin(); it != m_objects.end();it++)
		{
			(*it)->SerializeXML(ar);
		}
	}
	else
	{
		CString strType;
		CPGLNodePtr pObject;
		CPGLObjectPtr pPGLObject;
		
		xml.ResetChildPos();
		xml.SavePos(strPos);
		while ( xml.FindChildElem() )
		{
			pObject=pgl::CObjectRegistrySingleton::GetObject(xml.GetChildTagName());
			if (pObject)
			{
				if (pObject->IsKindOf(RUNTIME_CLASS(CPGLObject)))
				{
					pPGLObject=boost::shared_dynamic_cast<CPGLObject, CObject>( pObject );
					xml.RestorePos(strPos);
					pPGLObject->SerializeXML(ar);
					pPGLObject->SetParent( GetParent() );
					m_objects.push_back(pPGLObject);
					xml.SavePos(strPos);
				}
			} 
		}
	}
}

void CPGLRegionManager::SerializeXML(CPGLArchive& ar)
{
	CString strPos;	
	strPos.Format(_T("regionPos%u"),(DWORD)this);

	Container::iterator it;

	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if (ar.IsStoring())
	{
		// copying
		for (it = m_objects.begin(); it != m_objects.end();it++)
		{
			(*it)->SerializeXML(ar);
		}
	}
	else
	{
		CString strType;
		CPGLNodePtr pObject;
		CPGLRegionPtr pPGLRegion;
		
		xml.ResetChildPos();
		xml.SavePos(strPos);
		while ( xml.FindChildElem() )
		{
			pObject=pgl::CObjectRegistrySingleton::GetObject(xml.GetChildTagName());
			if (pObject)
			{
				if (pObject->IsKindOf(RUNTIME_CLASS(CPGLRegion)))
				{
					pPGLRegion=boost::shared_polymorphic_cast<CPGLRegion, CObject>( pObject );
					xml.RestorePos(strPos);
					pPGLRegion->SerializeXML(ar);
					pPGLRegion->SetParent( GetParent() );
					m_objects.push_back(pPGLRegion);
					xml.SavePos(strPos);
				}
			} 
		}
	}
}

long CPGLRegionManager::FindPointedRegion(gfxinterface::CGfxInterface &gfx, const CPGLPointD &point)
{
	UINT uRegionID=0;
	Container::iterator it;

	for (it=begin();it!=end();++it)
	{
		uRegionID=(*it)->FindPointedRegion(gfx, point);
		if (uRegionID)
			return uRegionID;
	}
	return 0;
}

void CPGLObjectManager::PushLinesTop(CPGLObjectManager::LegendPtr pLegend)
{
	if (!pLegend)
		return;

	// adding object...
	Container::iterator it;
	CPGLObjectPtr pObject;

	for (it=begin();it!=end();++it)
	{
		pObject=*it;

		if (pObject->IsKindOf(RUNTIME_CLASS(CPGLStackedLine2D)))
			((CPGLStackedLine2D*)&(*pObject))->PushLinesTop(pLegend);
		else if (pObject->IsKindOf(RUNTIME_CLASS(CPGLContainer)))
			((CPGLContainer*)&(*pObject))->PushLinesTop(pLegend);
		else if (pObject->IsKindOf(RUNTIME_CLASS(CPGLPie)))
			((CPGLPie*)&(*pObject))->PushLinesTop(pLegend);
		else if (pObject->IsKindOf(RUNTIME_CLASS(CPGLBar)))
			((CPGLBar*)&(*pObject))->PushLinesTop(pLegend);
		else
			pLegend->PushTop(pObject);

	}	
}

